/**
 * 文章controller（控制）层
 * @name: article.controller
 * @data: 2024/4/11 9:48
 */

import {Body, Controller, Del, Get, Inject, Param, Post} from "@midwayjs/core";
import {ArticleService} from "../service/article.service";
import Article from "../pojo/entity/article.entity";
import ResultInterface from "../interface/Result";
import {ArticleInsertVO, ArticleUpdateVO} from "../pojo/vo/article.vo";
import {HTTP_STATE_CODE} from "../common/const";
import {Context} from "@midwayjs/koa";
import {Validate} from "@midwayjs/validate";
import Result from "../common/Result";

@Controller("/article")
export class ArticleController {
  @Inject()
  articleService: ArticleService;
  @Inject()
  ctx: Context;

  /**
   * 新增文章
   * @param article
   */
  @Post("/insert")
  // @ts-ignore
  @Validate(ArticleInsertVO)
  async insertArticle(@Body() article: Article): Promise<ResultInterface<Article>> {
    try {
      //进行验证
      const articleData = ArticleInsertVO.parse(article);
      // console.log(24, articleData);
      const result = await this.articleService.insertArticle(articleData as Article);
      // const result = await this.articleService.insertArticle(article);
      if (result) {
        return Result<Article>({
          code: HTTP_STATE_CODE.SUCCESS,
          msg: "新增成功",
        });
      } else {
        return Result<Article>({
          code: HTTP_STATE_CODE.ERROR,
          msg: "新增失败",
        });
      }
    } catch (e) {
      return {
        code: HTTP_STATE_CODE.FORBIDDEN,
        msg: "缺少必填字段",
        data: null
      };
    }
  }

  /**
   * 删除文章
   * @param articleId
   */
  @Del("/:articleId")
  async deleteArticle(@Param("articleId") articleId: number): Promise<ResultInterface<Article>> {
    try {
      const result = await this.articleService.deleteArticleById(articleId);
      if (result.affected > 0) {
        return Result<Article>({msg: "删除成功", code: HTTP_STATE_CODE.SUCCESS});
      } else {
        return Result<Article>({msg: "删除失败", code: HTTP_STATE_CODE.FORBIDDEN});
      }
    } catch (e) {
      return Result<Article>({msg: "删除失败", code: HTTP_STATE_CODE.ERROR});
    }

  }

  /**
   * 更新文章
   */
  @Post("/update")
  async updateArticle(@Body() article: Article): Promise<ResultInterface<Article>> {
    try {
      const articleData = ArticleUpdateVO.parse(article);
      const result: any = await this.articleService.updateArticle(articleData as Article);
      console.log(80, result);
      if (result.changedRows > 0) {
        return Result<Article>({
          code: HTTP_STATE_CODE.SUCCESS,
          msg: "修改成功",
        });
      } else {
        return Result<Article>({
          code: HTTP_STATE_CODE.FORBIDDEN,
          msg: "修改失败",
        });
      }
    } catch (e) {
      console.log(92, e);
      return {
        code: HTTP_STATE_CODE.FORBIDDEN,
        msg: "缺少必填字段",
        data: e
      };
    }
  }

  /**
   * 根据作者id查询文章列表
   * @param authorId
   */
  @Get("/authorId/:authorId")
  async selectArticleByAuthorId(@Param("authorId") authorId: number): Promise<ResultInterface<Article>> {
    try {
      const result = await this.articleService.selectArticleByAuthorId(authorId);
      if (result.length > 0) {
        return Result<Article>({
          code: HTTP_STATE_CODE.SUCCESS,
          msg: "查询成功",
          data: result
        });
      } else {
        return Result<Article>({
          code: HTTP_STATE_CODE.NO_CONTENT,
          msg: "查询成功",
          data: result
        });
      }
    } catch (e) {
      return Result<any>({
        code: HTTP_STATE_CODE.FORBIDDEN,
        msg: "查询失败",
        data: e
      });
    }
  }

  /**
   * 根据文章title查询文章列表
   * 支持模糊查询
   * @param title
   */
  @Get("/title/:title")
  async selectArticleByTitle(@Param("title") title: string): Promise<ResultInterface<Article>> {
    try {
      const result=await this.articleService.selectArticleByTitle(title);
      if (result.length > 0) {
        return Result<Article>({
          code: HTTP_STATE_CODE.SUCCESS,
          msg: "查询成功",
          data: result
        });
      } else {
        return Result<Article>({
          code: HTTP_STATE_CODE.NO_CONTENT,
          msg: "查询失败",
          data: result
        });
      }
    }catch (e) {
      return Result<any>({
        code: HTTP_STATE_CODE.ERROR,
        msg: "查询失败",
        data: e
      });
    }
  }

  /**
   * 返回全部文章,无分页
   */
  @Get("/select")
  async selectAllArticle(): Promise<ResultInterface<Article>>{
    try {
      const result = await this.articleService.selectAllArticle();
      if (result.length > 0) {
        return Result<Article>({
          code: HTTP_STATE_CODE.SUCCESS,
          msg: "查询成功",
          data: result
        });
      } else {
        return Result<Article>({
          code: HTTP_STATE_CODE.NO_CONTENT,
          msg: "查询成功",
          data: result
        });
      }
    } catch (e) {
      return Result<any>({
        code: HTTP_STATE_CODE.ERROR,
        msg: "查询失败",
        data: e
      });
    }
  }

  /**
   * 增加文章喜欢数
   */
  @Get("/addLikeCount/:articleId")
  async insertLikeCountById(@Param("articleId") articleId:number): Promise<ResultInterface<Article>> {
    try {
      const result= await this.articleService.insertLikeCountById(articleId);
      // console.log(204, result);
      if (result. affectedRows>0){
        return Result<any>({
          code: HTTP_STATE_CODE.SUCCESS,
          msg: "新增成功",
        });
      }else{
        return Result<any>({
          code: HTTP_STATE_CODE.FORBIDDEN,
          msg: "新增失败",
        });
      }
    }catch (e) {
      return Result<any>({
        code: HTTP_STATE_CODE.ERROR,
        msg: "查询失败",
        data: e
      });
    }
  }

  /**
   * 删除文章喜欢数
   */
  @Get("/deleteLickCount/:articleId")
  async deleteLikeCountById(@Param("articleId") articleId:number): Promise<ResultInterface<Article>> {
    try {
      const result= await this.articleService.deleteLikeCountById(articleId);
      // console.log(204, result);
      if (result. affectedRows>0){
        return Result<any>({
          code: HTTP_STATE_CODE.SUCCESS,
          msg: "删除成功",
        });
      }else{
        return Result<any>({
          code: HTTP_STATE_CODE.FORBIDDEN,
          msg: "删除失败",
        });
      }
    }catch (e) {
      return Result<any>({
        code: HTTP_STATE_CODE.ERROR,
        msg: "查询失败",
        data: e
      });
    }
  }

}
